package main;

import org.quartz.CronScheduleBuilder;
import org.quartz.DateBuilder;

import pojo.Schedule;

public class CronScheduleBuilderFactory {
	public CronScheduleBuilder returnNewCronScheduleBuilder(Schedule s){
		switch(s.getHowOftenIndex()){
		case 0:
			return daily(s.getWhatTimeIndex());
		case 1:
			return weekly(s.getWhatDayIndex(), s.getWhatTimeIndex());
		case 2:
			return monthly(s.getWhatDayIndex(), s.getWhatTimeIndex());
		}
		return null;
	}
	private  CronScheduleBuilder daily(int whatTimeIndex){
		return CronScheduleBuilder.dailyAtHourAndMinute(whatTimeIndex, 0);
	}
	private  CronScheduleBuilder weekly(int whatDayIndex, int whatTimeIndex){
		CronScheduleBuilder temp = null;
		switch(whatDayIndex){
		case 0:
			temp = CronScheduleBuilder.weeklyOnDayAndHourAndMinute(DateBuilder.SUNDAY, whatTimeIndex, 0);
			break;
		case 1:
			temp = CronScheduleBuilder.weeklyOnDayAndHourAndMinute(DateBuilder.MONDAY, whatTimeIndex, 0);
			break;
		case 2:
			temp = CronScheduleBuilder.weeklyOnDayAndHourAndMinute(DateBuilder.TUESDAY, whatTimeIndex, 0);
			break;
		case 3:
			temp = CronScheduleBuilder.weeklyOnDayAndHourAndMinute(DateBuilder.WEDNESDAY, whatTimeIndex, 0);
			break;
		case 4:
			temp = CronScheduleBuilder.weeklyOnDayAndHourAndMinute(DateBuilder.THURSDAY, whatTimeIndex, 0);
			break;
		case 5:
			temp = CronScheduleBuilder.weeklyOnDayAndHourAndMinute(DateBuilder.FRIDAY, whatTimeIndex, 0);
			break;
		case 6:
			temp = CronScheduleBuilder.weeklyOnDayAndHourAndMinute(DateBuilder.SATURDAY, whatTimeIndex, 0);
			break;
		}
		return temp;
	}
	private  CronScheduleBuilder monthly(int whatDayIndex, int whatTimeIndex){
		if(whatDayIndex == 31){
			String cronExpression = "0 0 " + String.valueOf(whatTimeIndex) + " L * ?";
			return CronScheduleBuilder.cronSchedule(cronExpression);
		}
		return CronScheduleBuilder.monthlyOnDayAndHourAndMinute(whatDayIndex + 1, whatTimeIndex, 0);
	}
}
